import Operator from "./Operator";
import {RC} from "../config/RC";
import MessageBox from "../entities/MessageBox";
import Users from "../entities/User";
import Group from "../entities/Group";
import MessageBoxForGroup from "../entities/MessageBoxForGroup";
import MessageBoxForSingleUser from "../entities/MessageBoxForSingleUser";
import {GROUP, USER} from "../config/config";

class GetMessageOperator extends Operator{
    public async execute(): Promise<RC> {
        const userId = this.args.getProperty('userId');
        const boxType = this.args.getProperty('boxType');
        let messageBox : MessageBox | null = null;
        if(boxType === USER){
            messageBox = new MessageBoxForSingleUser(boxType, userId);
        }else if(boxType === GROUP){
            messageBox = new MessageBoxForGroup(boxType, userId);
        }
        if(!messageBox){
            return RC.USERID_NOTEXIST;
        }
        const messages = await messageBox.getNewMessage();
        this.args.setProperty("messages",messages);
        return RC.SUCCESS;
    }
}

export default GetMessageOperator;